package cn.xhh.dailyreview.server.model;

import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.apache.commons.lang.StringUtils;

import cn.xhh.dailyreview.server.utils.Utility;

/**
 * The model class: User
 * @author cooky
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class User {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;

	@Persistent
	private String email;

	@Persistent
	private String password;
	
	@Persistent
	private Date creationTime;
	
	@Persistent
	private Boolean confirmed;
	
	@Persistent
	private String confirmStr;

	@Persistent
	private Integer privilege;
	
	public User(String email) {
		this.email = email;
		this.creationTime = new Date();
		this.confirmed = false;
		this.confirmStr = Utility.genConfirmString();
		this.privilege = 0;
	}
	
	public User(String email, String password) {
		this(email);
		this.password = password;
	}

	public Long getId() {
		return id;
	}
	
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Date getCreationTime() {
		return creationTime;
	}

	public void setCreationTime(Date creationTime) {
		this.creationTime = creationTime;
	}
	public boolean isConfirmed() {
		return confirmed;
	}

	public void setConfirmed(Boolean confirmed) {
		this.confirmed = confirmed;
	}

	public String getConfirmStr() {
		return confirmStr;
	}

	public void setConfirmStr(String confirmStr) {
		this.confirmStr = confirmStr;
	}

	public int getPrivilege() {
		return privilege;
	}

	public void setPrivilege(int privilege) {
		this.privilege = privilege;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj==null || !(obj instanceof User))
			return false;
		
		User u = (User) obj;
		if (!Utility.isEqual(email, u.getEmail())) return false;
		if (!Utility.isEqual(password, u.getPassword())) return false;
		if (!Utility.isEqual(creationTime, u.getCreationTime())) return false;
		if (!Utility.isEqual(confirmed, u.isConfirmed())) return false;
		if (!Utility.isEqual(confirmStr, u.getConfirmStr())) return false;
		if (!Utility.isEqual(privilege, u.getPrivilege())) return false;
		
		return true;
	}
	
	@Override
	public int hashCode() {
		int hash = 7;
		hash = 31 * hash + (null == email ? 0 : email.hashCode());
		hash = 31 * hash + (null == password ? 0 : password.hashCode());
		hash = 31 * hash + (null == creationTime ? 0 : creationTime.hashCode());
		hash = 31 * hash + (null == confirmed ? 0 : (confirmed ? 1 : 0));
		hash = 31 * hash + (null == confirmStr ? 0 : confirmStr.hashCode());
		hash += privilege;
		return hash;
	}
	
	@Override
	public String toString() {
		return getEmail() + " (joined at " 
			+ (creationTime==null ? "null" : Utility.getDateFormat().format(creationTime)) 
			+ ")";
	}
	
	public boolean valid() {
		return (Utility.isEmail(email) || "admin".equals(email) || "demo".equals(email)) // FIXME
			&& creationTime != null
			&& (confirmed || StringUtils.isNotBlank(confirmStr));
	}
	
	public boolean invalid() {
		return !valid();
	}

}
